#include "sms.h"

///////////////////////////////
//static 



///////////////////////////////
//API

Button *button_new(GooCanvasItem * parent,int x,int y,Theme *theme)
{
	int w,h;
	Button *button;
	
	if(!theme||!parent)
		return NULL;
	
	button=(Button *)g_malloc0(sizeof(Button)+1);
	button->x=x;
	button->y=y;	
	button->width=gdk_pixbuf_get_width(theme->pic[THEME_PIC_BUTTON_FG]);
	button->height=gdk_pixbuf_get_height(theme->pic[THEME_PIC_BUTTON_FG]);
	button->parent=parent;

	button->cavas=goo_canvas_group_new(parent,"x",button->x, "y",button->y,"width",button->width,"height",button->height, NULL);

	button->background=goo_canvas_image_new (button->cavas, theme->pic[THEME_PIC_BUTTON_BG], 0, 0, NULL);
	
	button->foreground=goo_canvas_image_new (button->cavas, theme->pic[THEME_PIC_BUTTON_FG], 0, 0, NULL);
		
	button->text=goo_canvas_text_new(button->cavas,"",
									 button->width/2,button->height/2,
									 -1,GTK_ANCHOR_CENTER,
									"font",theme->font[THEME_FONT_SMALL],
									NULL);

	button->icon_fg=goo_canvas_image_new (button->cavas, NULL, 0, 0, NULL);
	button->icon_bg=goo_canvas_image_new (button->cavas, NULL, 0, 0, NULL);
	
	button->theme=theme;
	
	button->focus=TRUE;
	button_set_focus(button,FALSE);
	return button;
}

Button *button_new_with_text(GooCanvasItem * parent,int x,int y,Theme *theme,const char *text)
{
	Button *button=button_new(parent, x, y,theme);
	if(button)
		button_set_text(button,text);
	return button;
}

void button_destroy(Button *button)
{
	if(button->background)
		g_object_unref(button->background);
	if(button->foreground)
		g_object_unref(button->foreground);
	if(button->text)
		g_object_unref(button->text);
	if(button->icon_bg)
		g_object_unref(button->icon_bg);	
	if(button->icon_fg)
		g_object_unref(button->icon_fg);
	if(button->cavas)
		g_object_unref(button->cavas);
	g_free(button);
}

void button_set_propertie(Button *button,const char *key,gpointer data)
{
	
}

void button_set_func(Button *button,const char *key,gboolean (*func)(Button *button,gpointer data),gpointer user_data)
{
	if(g_strcmp0(key,"clicked")==0)
	{
		button->clicked_func=func;
		if(user_data)
			g_object_set_data(G_OBJECT(button->cavas),"clicked-data",user_data);
	}
}

gboolean button_get_focus(Button *button)
{
	return button->focus;
}
	
void button_set_focus(Button *button,gboolean focus)
{
	if(button->focus==focus)
		return;
	button->focus=focus;
	if(focus)
	{
		g_object_set(button->foreground,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		if(button->icon_bg)	
			g_object_set(button->icon_bg,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
		if(button->icon_fg)	
			g_object_set(button->icon_fg,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		
		g_object_set(button->text,"fill_color",button->theme->color[THEME_COLOR_TEXT_ACTIVE],NULL);
		
	}
	else
	{
		g_object_set(button->foreground,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
		if(button->icon_bg)	
			g_object_set(button->icon_bg,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		if(button->icon_fg)	
			g_object_set(button->icon_fg,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
		
		g_object_set(button->text,"fill_color",button->theme->color[THEME_COLOR_TEXT_DISACTIVE],NULL);
	}
}

gboolean button_click(Button *button)
{
	if(button->clicked_func)
	{		
		gpointer data=g_object_get_data(G_OBJECT(button->cavas),"clicked-data");
		return button->clicked_func(button,data);
	}
	return TRUE;
}

void button_set_text(Button *button ,const char *text)
{
	g_object_set(button->text,"text",text,NULL);
}

const char *button_get_text(Button * button)
{
	char *text;
	g_object_get(button->text,"text",&text,NULL);
	return (const char *)text;
}

void button_set_icon(Button *button,GdkPixbuf *fg,GdkPixbuf *bg)
{
	int w=0,h=0,w1=0,h1=0,b;
	gdouble text_x;

	if(bg)
	{
		w=gdk_pixbuf_get_width(bg);
		h=gdk_pixbuf_get_height(bg);
		b=(button->y-h)/2;
		if(b<0)
			b=0;
		//g_object_set(G_OBJECT(button->icon_bg),"pixbuf",bg,NULL);
		//button->foreground=goo_canvas_image_new (button->cavas, bg, b, b, NULL);
	}

	g_object_set(G_OBJECT(button->icon_bg),"pixbuf",bg,NULL);
	
	if(fg)
	{
		w1=gdk_pixbuf_get_width(fg);
		h1=gdk_pixbuf_get_height(fg);
		
		if(h1>h)
		{
			b=(button->y-h1)/2;
			if(b<0)
				b=0;
		}		
		//button->foreground=goo_canvas_image_new (button->cavas, bg, b, b, NULL);
	}

	g_object_set(G_OBJECT(button->icon_fg),"pixbuf",fg,NULL);

	if(w1>0||w>0)
	{
		if(w1>w)
			button->icon_area=w1+b*2;
		else
			button->icon_area=w+b*2;
		
		text_x=(gdouble)((button->width-button->icon_area)/2+button->icon_area);
		g_object_set(button->text,"x",text_x,NULL);	
	}
}

void button_set_ground(Button *button,GdkPixbuf *fg,GdkPixbuf *bg)
{
	int w=0,h=0,w1=0,h1=0;
	if(bg)
	{
		w=gdk_pixbuf_get_width(bg);
		h=gdk_pixbuf_get_height(bg);
	}
	if(fg)
	{
		w1=gdk_pixbuf_get_width(fg);
		h1=gdk_pixbuf_get_height(fg);
	}
	
	button->width=w>w1?w:w1;
	button->height=h>h1?h:h1;
	
	g_object_set(G_OBJECT(button->background),"pixbuf",bg,NULL);	
	g_object_set(G_OBJECT(button->foreground),"pixbuf",fg,NULL);	
	
	g_object_set(G_OBJECT(button->cavas),"width",button->width,"height",button->height,NULL);	
}

void button_set_size(Button *button,int width,int height)
{
	button->width=width;
	button->height=height;
	g_object_set(button->cavas,"width",button->width,
				 "height",button->height,NULL);
	g_object_set(button->text,"x",(button->width-button->icon_area)/2,
				 "y",(button->height)/2,NULL);
	
}
